package com.jpaAutoProcess.utils.parseParamCondition;

import com.jpaAutoProcess.utils.ParamConditionBO;
import com.jpaAutoProcess.utils.type.Condition;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: c.c
 * @Date: 2022/6/9
 * @Time: 11:28
 * @Description: 自动分页需要的类 解析条件
 */

public class RequirParametersAndConditions<K,V> extends HashMap<String,Object> {
     static private RequirParametersAndConditions<ParamConditionBO,Object> paramConditionBOObjectRequirParametersAndConditions =null;

    public final static RequirParametersAndConditions<ParamConditionBO,Object> getInstance(){
        paramConditionBOObjectRequirParametersAndConditions = new RequirParametersAndConditions<ParamConditionBO,Object>();
        return paramConditionBOObjectRequirParametersAndConditions;
    }

    /**
    * @author: C.c
    * @date: 2022-06-09 11:41
    * @description:
     * @param sqlparamAndContion 字段属性_判断条件 使用:  xxx.put(new ParamConditionBO(“m.name”,Conditon.like),Value);
     * @param value 需要查询或需要比较的值
     * @Deprecated use {@link #initParamAndCondition(String, Condition, Object)}} 这个方法用initParamAndCondition代替
    **/
    @Deprecated
    public Object put(ParamConditionBO sqlparamAndContion, Object value) {

        String paramAndContion = ParamConditionBO.initparam(sqlparamAndContion.getPropertyName(), sqlparamAndContion.getCondition());

        //若发现重复的条件，转换成数组
        Object o = this.putIfAbsent(paramAndContion, value);
        if (o!=null){
            List<Object> params  = new ArrayList<Object>();
            params.add(o);
            params.add(value);
            this.put(paramAndContion,params);
        }
        return super.put(paramAndContion, value);
    }

    /**
     * @author: C.c
     * @date: 2022-06-19 11:41
     * @description:
     * @param whereParam 需要比较的字段的属性 如 selet t.name , ys.age from xxx where t.name= :nameValue 那么“whereParam”就是  t.name
     * @param condition 判断条件 如 not：！= ，eq:== ，ge:> , le:< ，用封装好的<code>condition</code>这个枚举
     * @param value 需要查询或需要比较的值
     **/
    public RequirParametersAndConditions<K,V> initParamAndCondition(String whereParam, Condition condition,Object value){
        put(new ParamConditionBO(whereParam,condition),value);
        return this;
    }
}
